package com.orkun.api.converter.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.file.Paths;

/*
 * Utility methods used in API
 */
public class Utility {

  /*
   * Streams from a file. Make sure that the res folder is in the buildpath.
   */
  public static InputStream getInputStreamFromFile(String path) {    
    InputStream is = Utility.class.getClassLoader().getResourceAsStream(path);
    return is;
  }

  public static Charset getEncoding() {
    // ISO-8859-9 ISO-8859-1 UTF-8
    return Charset.forName("UTF-8");
  }

  /*
   * Creates a file in the path with the given content
   */
  public static void writeToFile(String path, String content) {
    PrintWriter writer;
    try {
      writer = new PrintWriter(Paths.get(path).toString(), getEncoding().toString());
      writer.print(content);
      writer.close();
    } catch (FileNotFoundException | UnsupportedEncodingException e) {
      e.printStackTrace();
    }
  }
  
  public static File getFile(String path) throws IOException
  {
    File output = new File(path);
    if (!output.exists()) {
      output.createNewFile();
    }
    return output;
  }


}
